package org.eclipse.stp.policy_builder.model.source.wizards.ui;

import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.stp.policy_builder.Activator;
import org.eclipse.stp.policy_builder.model.source.SourceExtensionParser;
import org.eclipse.stp.policy_builder.model.source.SourceType;
import org.eclipse.stp.policy_builder.model.source.wizards.MessageContentProvider;
import org.eclipse.stp.policy_builder.model.source.wizards.MessageLabelProvider;
import org.eclipse.stp.policy_builder.model.source.wizards.SourceLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class MessageSourcesPage extends WizardPage {
	private CheckboxTableViewer checkboxTableMessageViewer;
	private final static String COL_LABEL_MSG_SOURCE = "Message Source";
	private final static String COL_LABEL_MSG_ID = "ID";
	private final static String COL_LABEL_MSG_DESC = "Description";
	
	public MessageSourcesPage(){
		super("selectMessages");
		setTitle("Select Messages to begin Policy Extraction :");
		setDescription("Messages Selected from the following will be used for the Policy extraction....");
	} 
	@Override
	public void createControl(Composite parent) {
		// TODO Auto-generated method stub
		 Composite container = new Composite(parent, SWT.NULL);
	     container.setLayout(new FormLayout());
	     setControl(container);
	     
	      checkboxTableMessageViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER);
	      checkboxTableMessageViewer.addSelectionChangedListener(new ISelectionChangedListener(){
	        
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				// TODO Auto-generated method stub
				//updatePageButtons();
				
			}
	    	  
	      });
	    
	      
		         
	      //checkboxTableMessageViewer.setInput(new SourceExtensionParser<SourceType>(Activator.PLUGIN_ID,"source"));
	      final Table table = checkboxTableMessageViewer.getTable();
	      final FormData formData = new FormData();
	      formData.bottom = new FormAttachment(100, 0);
	      formData.right = new FormAttachment(100, 0);
	      formData.top = new FormAttachment(0, 0);
	      formData.left = new FormAttachment(0, 0);
	      table.setLayoutData(formData);
	      table.setHeaderVisible(true);

	      final TableColumn tableColumn = new TableColumn(table, SWT.NONE);
	      tableColumn.setWidth(200);
	      tableColumn.setText(COL_LABEL_MSG_ID);

	      final TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
	      tableColumn_1.setWidth(150);
	      tableColumn_1.setText(COL_LABEL_MSG_SOURCE);
	      
	      final TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
	      tableColumn_2.setWidth(250);
	      tableColumn_2.setText(COL_LABEL_MSG_DESC);
	      
	      checkboxTableMessageViewer.setContentProvider(new MessageContentProvider());
	      checkboxTableMessageViewer.setLabelProvider(new MessageLabelProvider());
	      //SourceType type = getSelectedTypes();
	     // checkboxTableMessageViewer.setInput(type);
	}
	
	public void setVisible(boolean visible){
		
		if(visible){
			SourceType type = getSelectedTypes();
			checkboxTableMessageViewer.setInput(type);
		}
		super.setVisible(visible);
		
	}
	
	private SourceType getSelectedTypes() {
		// TODO Auto-generated method stub
        return ((PolicyBuilderWizard) getWizard()).getSelectedTypes();
	}
}
